package echat.future.cn.echat.my.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityContributionBinding;
import echat.future.cn.echat.my.adapter.ContributionAdapter;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.ContributionInfo;
import echat.future.cn.echat.my.bean.ReachrgeListBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.StatusBarUtils;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 贡献值
 */
public class ContributionActivity extends BaseActivity<ActivityContributionBinding> {


    ContributionAdapter contributionAdapter;

    List<ReachrgeListBean> mDatas = new ArrayList<>();

    @Override
    public int layoutId() {
        return R.layout.activity_contribution;
    }

    @Override
    public IBaseNavagation navagation() {
        return null;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.hideStatus(this);
    }

    @Override
    public void initView() {
        contributionAdapter = new ContributionAdapter(mDatas);
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        bind.recyclerView.setAdapter(contributionAdapter);
        loadPage(1);
        contributionAdapter.setOnLoadMoreListener(() -> {
            page++;
            loadPage(page);
        }, bind.recyclerView);
    }


    int page = 1;

    private void loadPage(int page) {
        RequestBody token = RequestBody.create(MediaType.parse("multipart/form-data"), UserHelper.getToken());
        RequestBody p = RequestBody.create(MediaType.parse("multipart/form-data"), page + "");
        new HttpUtilsRequst<ContributionInfo>(this, HttpUtilsRequst.retrofitClient.create(UserApi.class).getUserContribution(token, p))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<ContributionInfo>) o -> {
                    contributionAdapter.loadMoreComplete();
                    bind.all.setText(o.getContribution_total());
                    bind.banlance.setText(o.getContribution_left());
                    bind.diving.setText(o.getGetDayTop());
                    mDatas.addAll(o.getList());
                    contributionAdapter.notifyDataSetChanged();
                    if(o.getList() == null || o.getList().size()<10){
                        contributionAdapter.loadMoreEnd();
                    }
                })
                .start();
    }
}
